test.c
#include"book.h"       
//顺序表的初始化
void Sq_List_Init(Sq_List* ps,int initcapacity)
{
        assert(ps);
        initcapacity = (initcapacity <= 0 ? 1 : initcapacity);//保证初始空间元素最少为1
        ps->array = (int*)malloc(initcapacity * sizeof(int));//此处的int均为type
        if (!ps->array) {
               perror("申请ps->array指向空间失败!\n");
               return;
        }
        ps->capacity = initcapacity;
        ps->cur_size = 0;
        printf("初始化成功!\n");
        return;
}
//检查顺序表是否需要扩容
void Check_Capacity(Sq_List* ps, int initcapacity)
{
        assert(ps);
        if (ps->cur_size == ps->capacity) {
               
               int* pps = realloc(ps->array, 2 * (ps->capacity) * sizeof(int));//int-》type
               if (!pps) {
                       perror("扩容失败!\n");
                       return;
        }
               ps->array = pps;
               ps->capacity *= 2;
               printf("扩容成功！\n");
               return;
        }
}
//顺序表尾插
void Sq_List_In_Back(Sq_List* ps, int initcapacity, int date)
{
        assert(ps);
        Check_Capacity(ps,initcapacity);//检查容量
        ps->array[ps->cur_size] = date;//插入元素 date
        ps->cur_size++;//有效元素+1
        printf("尾插成功!\n");
        return;
}
//顺序表尾删(有效元素为0时不执行)
void Sq_List_Del_Back(Sq_List* ps)
{
        assert(ps);
        if (Check_NULL(ps)) {
               printf("顺序表内无有效元素!\n");
               return;//若顺序表为空，则直接返回
        }
        ps->cur_size--;
        printf("尾删成功！\n");
        return;
}
//顺序表头插
void Sq_List_In_Head(Sq_List* ps, int initcapacity,int date)
{
        assert(ps);
        Check_Capacity(ps, initcapacity);//检查容量
        int tmp = ps->array[0];
        ps->array[0] = date;
        for (int i = ps->cur_size; i > 1 ; i--) {
               ps->array[i] = ps->array[i-1];
        }
        ps->array[1] = tmp;
        ps->cur_size++;
        printf("头插成功！\n");
        return;
}
//顺序表头删
void Sq_List_Del_Head(Sq_List* ps)
{
        assert(ps);
        if (Check_NULL(ps)) {
               printf("顺序表为空！\n");//无有效元素，不删除
        }
        for (size_t i = 0; i < ps->cur_size - 1; i++) {
               ps->array[i] = ps->array[i + 1];
        }
        ps->cur_size--;
        printf("头删成功！\n");
        return;
}
//顺序表任意位置删除
void Sq_List_Del_Ran(Sq_List* ps)
{
        assert(ps);
        int location;
        if (Check_NULL(ps)) {
               printf("顺序表为空！\n");//无有效元素，不删除
        }
        Sq_List_Print(ps);//先打印查看元素
        printf("请选择要删除的位置下标>>");
        scanf("%d", &location);
        for (size_t i = location; i < ps->cur_size - 1; i++) {
               ps->array[i] = ps->array[i + 1];
        }
        ps->cur_size--;
        printf("删除成功!\n");
        return;
}
//顺序表任意位置插入
void Sq_List_In_Ran(Sq_List* ps, int initcapacity, int date)
{
        int location, tmp;
        assert(ps);
        Check_Capacity(ps, initcapacity);//检查容量
        Sq_List_Print(ps);//打印查看元素
        printf("请选择要插入的位置下标>>");
        scanf("%d", &location);
        tmp = ps->array[location];
        ps->array[location] = date;
        for (int i = ps->cur_size; i > location + 1; i--) {
               ps->array[i] = ps->array[i - 1];
        }
        ps->array[location + 1] = tmp;
        ps->cur_size++;
        printf("插入成功!\n");
        return;
}
//顺序表查找(存在返回下标，不存在返回-1)
int Sq_List_Search(Sq_List* ps, int date)
{
        assert(ps);
        for (size_t i = 0; i < ps->cur_size; i++) {
               if (ps->array[i] == date) {
                       printf("该元素下标为%d\n", i);
                       return i;
               }
        }
        printf("查无此数！\n");
        return -1;
}
//顺序表销毁
void Sq_List_Destory(Sq_List* ps)
{
        assert(ps);
        if (ps->array) {
               free(ps->array);
               ps->array = NULL;
               ps->capacity = 0;
               ps->cur_size = 0;
        }
        return;
}
//顺序表打印
void Sq_List_Print(Sq_List* ps)
{
        assert(ps);
        for (size_t i = 0; i < ps->cur_size; i++){
               printf("%d ", ps->array[i]);
        }
        printf("\n");
        return;
}
//检查顺序表是否为空
int Check_NULL(Sq_List* ps)
{
        return (0 == ps->cur_size);//有效元素为空则返回1
}


